//
//  UIBarButtonItem+Extension.swift
//  LVWeibo
//
//  Created by lv on 15/6/29.
//  Copyright © 2015年 lv. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    // 便利构造器
    convenience init(imageName:String,highlightedImageName:String?) {

        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        // 设置高亮图片名称
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        // 自动调整大小取代frame
        btn.sizeToFit()
        self.init(customView:btn)
    }
    
    // 便利构造器
    convenience init(imageName:String,highlightedImageName:String?,target: AnyObject?,actionName: String?) {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        // 设置高亮图片名称
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        // 自动调整大小取代frame
        btn.sizeToFit()
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        self.init(customView:btn)
    }
}
